/*
描述：定义评论管理模块的相关后端接口
完成人：吴悠
完成时间: 2025/04/27
*/


package com.jiesenhe.qacommunity.controller;

import com.jiesenhe.qacommunity.pojo.Comment;
import com.jiesenhe.qacommunity.service.CommentService;
import com.jiesenhe.qacommunity.util.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.jiesenhe.qacommunity.pojo.Tag;
import java.util.Map;

import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    CommentService commentService;
    @PostMapping("/insertComment")
    public ResponseEntity<Object> insertComment(@RequestBody Comment comment, HttpServletRequest request){
        String token = request.getHeader("Token");
        if (!JwtUtil.checkToken(token)){
            return ResponseEntity.ok(new java.util.HashMap<String, Object>() {
                {
                    put("code", 0);
                    put("msg", "token check failed");
                    put("data", null);
                }
            });
        }
        int result = commentService.insertComment(comment);
        return ResponseEntity.ok(new java.util.HashMap<String, Object>() {
            {
                put("code", result != 0 ? 1 : 0);
                put("msg", result != 0 ? "insertComment success" : "insertComment failed");
                put("data", null);
            }
        });

    }
    @DeleteMapping("/deleteComment")
    public ResponseEntity<Object> deleteComment(@RequestBody Comment comment, HttpServletRequest request){
        String token = request.getHeader("Token");
        if (!JwtUtil.checkToken(token)){
            return ResponseEntity.ok(new java.util.HashMap<String, Object>() {
                {
                    put("code", 0);
                    put("msg", "token check failed");
                    put("data", null);
                }
            });
        }
        int result = commentService.deleteComment(comment);
        int result2 = commentService.deleteCommentLike(comment.getId());
        return ResponseEntity.ok(new java.util.HashMap<String, Object>() {
            {
                put("code", result != 0&&result2!= 0? 1 : 0);
                put("msg", result != 0&&result2!= 0 ? "deleteComment success" : "deleteComment failed");
                put("data", null);
            }
        });
    }
    @GetMapping("/getComment")
    public ResponseEntity<Object> getComment(Comment comment)
    {
        int user_id = comment.getUser_id();
        List<Comment> list = commentService.getComment(user_id);
        System.out.println(list+"---------------");
        return ResponseEntity.ok(new java.util.HashMap<String, Object>() {
            {
                put("code", (list != null && !list.isEmpty()) ? 1 : 0);
                put("msg", (list != null && !list.isEmpty()) ? "getComment success" : "getComment failed");
                put("data", list);
            }
        });
    }
}
